<?php

/**
 * 我的栏目
 */

namespace Controller;

class My extends \Controller\Base {

    private $co = '';

    /**
     * 构造函数
     */
    public function __construct($vals = array()) {
        parent::__construct($vals);
        $this->co = $this->getGet('co');
    }

    /**
     * 个人中心
     */
    public function index() {

        // 邀请人的openid
        $inviteid = $this->getGet('inviteid');
        if ($inviteid) {
            \Common::setCookie('inviteid', $inviteid);
        }

        // 是否跳转到微信授权页
        \Logic\Product::itemGoToWx('myindex', 0, '', $this->role, $this->co, '', $inviteid);

        $this->assign('type', 'my');
        $this->display('index/my');
    }

    /**
     * 我的订单
     */
    public function order() {
        // 是否跳转到微信授权页
        \Logic\Product::itemGoToWx('myorder', 0, '', $this->role, $this->co);

        $page = $this->getGet('page');
        $page = $page ? $page : 1;
        $oid = $this->getGet('oid');
        $this->assign('index_url', '/my/order');

        $type = $this->getGet('type');
        $type = $type ? $type : 'all';

        $turl = $burl = '/my/order?';

        $openid = \Common::getCookie('openid');
        $where = $openid ? "`openid`='$openid'" : "1 != 1";

        if ($oid) {
            $where .= " and `order_id`='$oid'";
        } else {
            if ($type == 'pay') {
                // 已付款
                $where .= " and `status`='4'";
                $turl .= "&type=$type";
            }
            if ($type == 'close') {
                // 已结算
                $where .= " and `status` in('5','6')";
                $turl .= "&type=$type";
            }
            if ($type == 'wait') {
                // 红包发放
                $where .= " and `user_id`<>'' and `status` = '5'";
                $turl .= "&type=$type";
            }
        }

        $resp = \Logic\ORC::orderGet($where, $page);

        $this->assign('orderList', $resp['data_list']);
        $this->assign('title', '我的订单');

        $this->assign('page', $page);
        $this->assign('total_page', $resp['total_page']);

        $this->assign('type', 'my');
        $this->assign('burl', $burl);
        $this->assign('turl', $turl);
        $this->assign('oid', $oid);
        $this->assign('tab_type', $type);

        $this->display('order/list');
    }

    /**
     * 我的红包, 已领红包/待领红包
     */
    public function packet() {

        // 是否跳转到微信授权页
        \Logic\Product::itemGoToWx('mypacket', 0, '', $this->role, $this->co);

        $type = $this->getGet('type');
        $type = $type ? $type : 'has'; // has=已领, wait=待领

        $page = $this->getGet('page');
        $page = $page ? $page : 1;
        $turl = $burl = '/my/packet?';

        $openid = \Common::getCookie('openid');
        $where = $openid ? "`openid`='$openid'" : "1 != 1";

        if ($type == 'has') {
            $where .= " and `status`='6'";
            $burl .= 'type=has';
        }
        if ($type == 'wait') {
            $where .= " and `status` in('4','5')";
            $burl .= 'type=wait';
        }

        $resp = \Logic\ORC::orderGet($where, $page);

        $this->assign('title', '我的红包');
        $this->assign('dataList', $resp['data_list']);
        $this->assign('page', $page);
        $this->assign('total_page', $resp['total_page']);
        $this->assign('packetString', $type == 'has' ? '已收红包' : '预收红包');
        $this->assign('burl', $burl);
        $this->assign('turl', $turl);
        $this->assign('type', $type);

        $this->display('order/packet');
    }

    /**
     * 我的邀请（邀请好友）
     */
    public function invite() {
        // 是否跳转到微信授权页
        \Logic\Product::itemGoToWx('myinvite', 0, '', $this->role, $this->co);

        $openid = \Common::getCookie('openid');
        $page = $this->getGet('page');
        $page = $page ? $page : 1;
        $limit = 20;
        $data_list = [];
        $total_page = 0;

        while (true) {
            if (!$openid) {
                break;
            }

            $offset = ($page - 1) * $limit;
            $where = "`inviter` = '$openid'";
            $total_count = \Db::instance()->count("select count(1) from `wx_user` where $where");
            $total_page = ceil($total_count / $limit);

            $sql = "select * from `wx_user` where $where order by `invittime` desc limit $limit offset $offset";
            $data_list = \Db::instance()->getList($sql);

            break;
        }

        $this->assign('page', $page);
        $this->assign('total_page', $total_page);
        $this->assign('data_list', $data_list);
        $this->assign('tab_type', 'user');
        $this->display('index/myinvite');
    }

    /**
     * 我的邀请红包
     */
    public function invitepacket() {
        // 是否跳转到微信授权页
        \Logic\Product::itemGoToWx('invitepacket', 0, '', $this->role, $this->co);
        $openid = \Common::getCookie('openid');
        $page = $this->getGet('page');
        $page = $page ? $page : 1;
        $limit = 20;
        $return = [];
        $total_page = 0;

        while (true) {
            if (!$openid) {
                break;
            }

            $offset = ($page - 1) * $limit;
            $where = "`inviteid` = '$openid'";
            $total_count = \Db::instance()->count("select count(1) from `invite_packet` where $where");
            $total_page = ceil($total_count / $limit);

            $sql = "select `id`,`inviteid`,`openid`,`num`,`status`,`packettime` from `invite_packet` where $where order by `id` desc limit $limit offset $offset";
            $data_list = \Db::instance()->getList($sql);

            if (!$data_list) {
                break;
            }

            $openids = array_column($data_list, 'openid');
            $instring = "('" . implode("','", $openids) . "')";
            $ulist = \Db::instance()->getList("select `id`,`openid`,`weixinid`,`nickname`,`headimgurl`,`allpacket`,`monpacket` from `wx_user` where `openid` in$instring", 'openid');

            foreach ($data_list as $k => $d) {
                $return[$k] = $d;
                $tmpopenid = $d['openid'];
                $uinfo = isset($ulist[$tmpopenid]) ? $ulist[$tmpopenid] : [];
                $return[$k]['uid'] = $uinfo ? $uinfo['id'] : '';
                $return[$k]['weixinid'] = $uinfo ? $uinfo['weixinid'] : '';
                $return[$k]['nickname'] = $uinfo ? $uinfo['nickname'] : '';
                $return[$k]['headimgurl'] = $uinfo ? $uinfo['headimgurl'] : '';
            }
            
            break;
        }

        $this->assign('page', $page);
        $this->assign('total_page', $total_page);
        $this->assign('data_list', $return);
        $this->assign('tab_type', 'packet');
        $this->display('index/invitepacket');
    }

}
